package com.yuyou.fn.educational.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.github.wens.framework.service.impl.SuperServiceImpl;
import com.github.wens.mybatisplus.examples.Example;
import com.github.wens.mybatisplus.plugins.Page;
import com.yuyou.fn.common.exception.BusException;
import com.yuyou.fn.common.util.BeanUtils;
import com.yuyou.fn.common.util.CriteriaUtils;
import com.yuyou.fn.common.util.TimeUtils;
import com.yuyou.fn.educational.discount.DiscountRegCourseClass;
import com.yuyou.fn.educational.discount.DiscountResultSet;
import com.yuyou.fn.educational.discount.ExecuteServiceProxy;
import com.yuyou.fn.educational.discount.Helper;
import com.yuyou.fn.educational.entity.Discount;
import com.yuyou.fn.educational.entity.DiscountRule;
import com.yuyou.fn.educational.mapper.DiscountRuleMapper;
import com.yuyou.fn.educational.service.*;
import com.yuyou.fn.educational.vo.DiscountRuleQueryVo;
import com.yuyou.fn.educational.vo.DiscountRuleSaveOrUpdateVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 *
 * DiscountRule 表数据服务层接口实现类
 *
 */
@Service
public class DiscountRuleServiceImpl extends SuperServiceImpl<DiscountRuleMapper, DiscountRule,Long> implements IDiscountRuleService {

    @Resource
    private IDiscountCategoryService discountCategoryService ;

    @Resource
    private IDiscountService discountService ;

    @Resource
    private IPeriodService periodService ;

    @Resource
    private IStudentRegService studentRegService ;

    @Resource
    private IExternalRegDetailService externalRegDetailService;



    @Override
    public Page<DiscountRule> findDiscountRuleByQuery(DiscountRuleQueryVo discountRuleQueryVo, int pageNo, int pageSize) {

        Example<DiscountRule> example = DiscountRule.createExample();
        Example.Criteria criteria = example.createCriteria();
        CriteriaUtils.fillCondition(criteria,discountRuleQueryVo);
        example.orderBy("createTime").desc();
        example.orderBy("name").desc();
        return findPageByExample(example,pageNo,pageSize );
    }

    @Override
    public void saveOrUpdateRule(DiscountRuleSaveOrUpdateVo discountRuleSaveOrUpdateVo) {

        DiscountRule discountRule = new DiscountRule();

        BeanUtils.copyProperties(discountRule,discountRuleSaveOrUpdateVo );

        if(discountRule.getCategoryId() != null ){
            discountRule.setCategoryName(discountCategoryService.findCategoryNameByCategoryId(discountRule.getCategoryId()));
        }

        discountRule.setUpdateTime(TimeUtils.now());
        this.checkRule(discountRule);
        if(discountRule.getDiscountRuleId() == null ){
            discountRule.setCreateTime(TimeUtils.now());
            insertSelective(discountRule);
        }else{
            updateSelectiveById(discountRule);
        }

    }

    private void checkRule(DiscountRule discountRule){
        if (StringUtils.isEmpty(discountRule.getName())){
            throw new BusException("名称不可为空");
        }
        Example<DiscountRule> example = DiscountRule.createExample();
        example.createCriteria().andEqualTo("name",discountRule.getName()).andEqualTo("categoryId",discountRule.getCategoryId());
        DiscountRule check=this.findOneByExample(example);
        if (check!=null && !check.getDiscountRuleId().equals(discountRule.getDiscountRuleId())){
            throw new BusException("该分类下规则名称已存在");
        }
    }

    @Override
    public List<DiscountRule> findAll() {
        return findListByExample(DiscountRule.createExample());
    }

    @Transactional
    @Override
    public void deleteRule(Long discountRuleId) {

        Example<Discount> example = Discount.createExample();
        example.createCriteria().andEqualTo("discountRuleId",discountRuleId);
        long l = discountService.countByExample(example);
        if(l > 0 ){
            throw new BusException("规则已被使用，不能删除");
        }
        deleteById(discountRuleId);
    }


    @Override
    public String testRule(Long discountRuleId, String params, String testData ) {

        List<DiscountRegCourseClass> regCourseClassList = JSONObject.parseObject(testData, new TypeReference<List<DiscountRegCourseClass>>() {
        });

        DiscountRule discountRule = findById(discountRuleId);
        ExecuteServiceProxy executeServiceProxy = new ExecuteServiceProxy(discountRule.getCalFunction(),params,new Helper(periodService,studentRegService,externalRegDetailService));
        DiscountResultSet resultSet = executeServiceProxy.execute(regCourseClassList);
        return JSONObject.toJSONString(resultSet,true) ;
    }
}